﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using System.Collections.Generic;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;

public partial class rpt_InternalInvoicePayment : System.Web.UI.Page
{

    #region "Private&Public Property"

    private int itemID;
    private int accountID;
    private int salesPersonId;
    private DateTime? accountStartDate= null;
    private DateTime? accountEndDate=null;
    private IInternalAccountingService internalAccountingService;
    private ISalesPersonService salesPersonService;
    DataTable dt = new DataTable("InternalAccountingReport");
    int[] accountType;
    int formal, notFormal, disClosed;
    private CrystalReportManager manager;

    public ISalesPersonService SalesPersonService
    {
        get { return salesPersonService; }
        set { salesPersonService = value; }
    }

    public IInternalAccountingService InternalAccountingService
    {
        get { return internalAccountingService; }
        set { internalAccountingService = value; }
    }

    #endregion

    #region "Private Method"

    private void checkControlsValues()
    {
        if (!String.IsNullOrEmpty(txtItemName.Text)) {
            Item item = InternalAccountingService.FindItemByName(txtItemName.Text);
            if ( item != null )
                itemID = item.Id;
        }
        
        if (ddlAccountName.SelectedIndex != -1)
            accountID = Convert.ToInt32(ddlAccountName.SelectedValue);

        if (!String.IsNullOrEmpty(txtSalesPerson.Text)) {
            SalesPerson salesPerson = SalesPersonService.FindByTitle(txtSalesPerson.Text);
            if ( salesPerson != null )
                salesPersonId = salesPerson.Id;
        }

        if (txtToAccountDate.Text.Length > 0)
        {
            accountEndDate = MyPersianDate.PersianToDateTime(txtToAccountDate.Text);
        }
        if (txtFromAccountDate.Text.Length > 0)
        {
            accountStartDate = MyPersianDate.PersianToDateTime(txtFromAccountDate.Text);
        }
        int chKey = 0;
        if (chkFormal.Checked) {formal = 1; chKey++;}
        if (chkNotFormal.Checked) {notFormal = 2; chKey++;}
        if (chkDisClosed.Checked) {disClosed = 3; chKey++;}
        if (chKey++ == 0)
        { formal = 1; notFormal = 2; disClosed = 3; }
        accountType = new int[] { formal, notFormal, disClosed };

    }

    private void search()
    {
        checkControlsValues();
        SalesPerson salePerson = SalesPersonService.FindById(salesPersonId);
        Item item = InternalAccountingService.GetItem(itemID);
        InternalAccount account = InternalAccountingService.GetAccount(accountID);
        IList<InternalAccounting> internalAccounting = InternalAccountingService.InternalAccountingReport(item, salePerson, account, accountStartDate, accountEndDate, accountType);

        loadCrystallReport(generateDataTable(internalAccounting));
    }

    private void loadCrystallReport(DataTable dt)
    {
        DataSet ds = new DataSet("InternalAccountingReport");
        ds.Tables.Add(dt);

        List<string> names = new List<string>();
        List<object> values = new List<object>();

        string connInfo = ConfigurationManager.ConnectionStrings[0].ConnectionString;
        manager = new CrystalReportManager("InternalAccountPayment", names, values);
        manager.Init(connInfo);
        manager.SetReportDataset(ds);
        crvInternalAccountPayment.ReportSource = manager.Report;
        crvInternalAccountPayment.DataBind();
    }

    private DataTable generateDataTable(IList<InternalAccounting> internalAccount)
    {
        if (dt.Columns.Count == 0)
        {
            DataColumn key = new DataColumn();
            DataColumn dc0 = new DataColumn();
            DataColumn dc1 = new DataColumn();
            DataColumn dc2 = new DataColumn();
            DataColumn dc3 = new DataColumn();
            DataColumn dc4 = new DataColumn();
            DataColumn dc5 = new DataColumn();
            DataColumn dc6 = new DataColumn();
            DataColumn dc7 = new DataColumn();
            DataColumn dc8 = new DataColumn();

            dc0.ColumnName = "AccountName";
            dc1.ColumnName = "Title";
            dc2.ColumnName = "ItemName";
            dc3.ColumnName = "Debit";
            dc4.ColumnName = "CreateDate";
            dc5.ColumnName = "ItemId";
            dc6.ColumnName = "Quantity";
            dc7.ColumnName = "Unit";
            dc8.ColumnName = "InvoiceId";
            key.ColumnName = "AccountId";
            dt.Columns.Add(dc0);
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            dt.Columns.Add(dc6);
            dt.Columns.Add(dc7);
            dt.Columns.Add(dc8);
            dt.Columns.Add(key);

        }
        DataRow dr;
        foreach (InternalAccounting InAcc in internalAccount)
        {
            dr = dt.NewRow();
            dr[0] = InAcc.AccountName;
            if(InAcc.SalesPerson !=null)
                dr[1] = InAcc.SalesPerson.Title;
            if (InAcc.Item != null)
            {
                dr[2] = InAcc.Item.Name;
                dr[5] = InAcc.Item.Id;
                dr[7] = InAcc.Item.Unit;
            }

            if (InAcc.Debit == null | InAcc.Debit == 0)
            {
                dr[3] = 0 - InAcc.Credit;
                dr[2] = "تخفیف";
            }
            else
                dr[3] = InAcc.Debit;

            dr[4] = MyPersianDate.DateTimeToPersian(InAcc.AccountingDate);
            dr[6] = InAcc.Quantity;
            dr[8] = InAcc.InvoiceNumber;
            

            if(InAcc.Account!=null)
               dr[9] = InAcc.Account.Id;
            dt.Rows.Add(dr);
        }
        return dt;
    }

    private void CheckFormalVisibility() {
        bool show = CommonHelper.ShowHiddenCustomers();
        pnlAccountingType.Visible = show;
    }

    #endregion

    #region "Event"

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckFormalVisibility();
        if (Page.IsPostBack)
            search();
    }

    protected void Page_Unload(object sender, EventArgs e) {
        if (manager != null)
            manager.Dispose();
    }

    #endregion

}
